//
//  AttentionDetailVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "AttentionDetailVC.h"
#import "TDJScrollAdView.h"
#import "WarringVC.h"

@interface AttentionDetailVC () <TDJScrollViewDataSource, TDJScrollViewDelegate> {
    TDJScrollAdView *srcView;
    
    NSMutableArray *pArrayUserPic;
}

@property (weak, nonatomic) IBOutlet UILabel *pLblName;
@property (weak, nonatomic) IBOutlet UIImageView *pImgSex;
@property (weak, nonatomic) IBOutlet UILabel *pLblAge;
@property (weak, nonatomic) IBOutlet UILabel *pLblTime;
@property (weak, nonatomic) IBOutlet UILabel *pLblAddress;
@property (weak, nonatomic) IBOutlet UILabel *pLblDescription;

@property (weak, nonatomic) IBOutlet UIView *pViewHeader;

@property (weak, nonatomic) IBOutlet UIButton *pBtnAttention;

@end

@implementation AttentionDetailVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initInfo
{
    [super initInfo];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"关注"];
    
    [self.pViewHeader setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    self.pTableView.tableHeaderView = self.pViewHeader;
    srcView = [[TDJScrollAdView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    [srcView setDataSource:self];
    [srcView setDelegate:self];
    [srcView.timerScroll invalidate];
    [self.pViewHeader addSubview:srcView];
    [srcView reloadData];
    
    if (_pUser) {
        [_pLblName setText:_pUser.nickname];
        [_pImgSex setImage:[UIImage imageNamed:[_pUser.userSex isEqualToString:@"1"]?@"sex_boy":@"sex_girl"]];
        [_pLblAge setText:_pUser.userAge];
        [_pLblTime setText:_pUser.activeTimeLast];
        [_pLblAddress setText:_pUser.address];
        [_pLblDescription setText:@"个性签名"];
        [_pBtnAttention setHidden:self.hiddenAttentionBtn];
        _strID = _pUser.uid;
        
        if (!pArrayUserPic) {
            pArrayUserPic = [NSMutableArray array];
        }
        [pArrayUserPic addObject:[NSString stringWithFormat:@"%@/api/common/file/imgGet?fid=%@", IP, _pUser.headImage]];
        
        [self requestUserAlbum:_pUser.uid];
    }
}

- (IBAction)btnPopAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnWarringAction:(UIButton *)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Warring" bundle:nil];
    WarringVC *warringVC = [storyboard instantiateViewControllerWithIdentifier:@"WarringVC"];
    warringVC.pStrID = _pUser.uid;
    [self.navigationController pushViewController:warringVC animated:YES];
}

- (IBAction)btnPraiseAction:(id)sender
{
    [SVProgressHUD showInfoWithStatus:@"您没有可以赠送的礼物!"];
//    [SVProgressHUD show];
//    [TDNetwork requestZambia:zambia_interface toUid:_strID complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
//        if (succeed) {
//            int intCode = [[dictionary objectForKey:@"ret"] intValue];
//            switch (intCode) {
//                case 1:
//                    [SVProgressHUD showSuccessWithStatus:@"发送成功"];
//                    break;
//                    
//                case 2:
//                    //2、	可赞数目为0
//                    [SVProgressHUD showErrorWithStatus:@"可送数目为0"];
//                    break;
//                    
//                case 3:
//                    //3、	一天之内，不能对一个人重复赞
//                    [SVProgressHUD showErrorWithStatus:@"一天之内，不能对一个人重复送"];
//                    break;
//                    
//                default:
//                    [SVProgressHUD dismiss];
//                    break;
//            }
//        } else {
//            [SVProgressHUD dismiss];
//        }
//    }];
}

- (IBAction)btnCancelAtteniconAction:(id)sender
{
    [SVProgressHUD show];
    [TDNetwork requestDeleteCollection:deleteCollection_interface toUid:_strID complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showImage:[UIImage imageNamed:@"Attention_cancel"] status:@"已取消关注" maskType:SVProgressHUDMaskTypeBlack];
            [self performSelector:@selector(btnPopAction:) withObject:nil afterDelay:0.5f];
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)setStrID:(NSString *)strID
{
    _strID = strID;
    [self requestUserAlbum:strID];
    [SVProgressHUD show];

    [TDNetwork requsetUseMessagePostUrl:useGetMessageIP uid:strID complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        [SVProgressHUD dismiss];
        if (succeed) {
            NSDictionary *dic = [dictionary valueForKey:@"res"];

            NSLog(@" 用户信息： %@",dic);
            User *user = [User defaultUser];
            if (dic) {
                user.uid            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                user.headImage      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                user.nickName       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                user.userAge        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                user.userSex        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                user.zambia         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                user.faceScore      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                user.fans           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                user.movieLabel     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                user.commentCount   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                user.commentStart   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                user.commentTotalScore = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                user.phone          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                user.pwd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                user.beReprotedCount= [NSString stringWithFormat:@"%@",[dic valueForKey:@"beReportedCount"]];
                user.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                user.breakCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                user.signature      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"signature"]];
                user.address        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                user.activeTime     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                [_pLblName setText:user.nickName];
                [_pImgSex setImage:[UIImage imageNamed:[user.userSex isEqualToString:@"1"]?@"sex_boy":@"sex_girl"]];
                [_pLblAge setText:user.userAge];
                [_pLblTime setText:user.activeTimeLast];
                [_pLblAddress setText:user.address];
                [_pLblDescription setText:@"个性签名"];
                [_pBtnAttention setHidden:self.hiddenAttentionBtn];
                
                if (!pArrayUserPic) {
                    pArrayUserPic = [NSMutableArray array];
                }
                [pArrayUserPic insertObject:[NSString stringWithFormat:@"%@%@", IP, user.headImage] atIndex:0];
                [srcView reloadData];
            }
        } else {
        }
    }];
}


#pragma mark - TDJScrollViewDataSource, TDJScrollViewDelegate

- (NSInteger)numberOfPages {
    return pArrayUserPic.count;
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    UIImageView *imgPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, srcView.frame.size.height)];
    [imgPic sd_setImageWithURL:[NSURL URLWithString:[pArrayUserPic objectAtIndex:index]]];
    
    return imgPic;
}

- (void)didClickPage:(TDJScrollAdView *)view atIndex:(NSInteger)index {
}


- (void)requestUserAlbum:(NSString *)uid
{
    [TDNetwork getUserAlbumImagePostUrl:getAlbumImageIP uid:[uid integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSDictionary *dict = [dictionary objectForKey:@"res"];
            @try {
                NSArray *array = [dict objectForKey:@"albumList"];
                if (!pArrayUserPic) {
                    pArrayUserPic = [NSMutableArray array];
                }
                for (NSDictionary *item in array) {
                    [pArrayUserPic addObject:[NSString stringWithFormat:@"%@/api/common/file/imgGet?fid=%@", IP, [item objectForKey:@"albumId"]]];
                }
            }
            @catch (NSException *exception) {}
            @finally {
                [srcView reloadData];
            }
        }
    }];
}

@end
